//
//  timeController.m
//  myMedicine1.0
//
//  Created by pollysoft on 16/7/13.
//  Copyright © 2016年 microi. All rights reserved.
//

#import "timeController.h"

@interface timeController ()
@property (weak, nonatomic) IBOutlet UIButton *timeButton;
@property (strong,nonatomic) NSString *timeString;
@property (nonatomic, strong) UIView *fatherView;// 传入的父控件, 确定datePicker往哪儿加
- (IBAction)ShowRollerDate:(id)sender;

@property (weak, nonatomic) IBOutlet UIButton *afterButton;
@property (weak, nonatomic) IBOutlet UIButton *beforeButton;
- (IBAction)beforeDayAction:(id)sender;
- (IBAction)afterDayAction:(id)sender;

@property (weak, nonatomic) IBOutlet UIView *timeView;
//@property (weak, nonatomic) IBOutlet UIView *myMedicineView;
@property (weak, nonatomic) IBOutlet UIButton *myMedicineLeftButton;
@property (weak, nonatomic) IBOutlet UIButton *myMedicineButton;
@property (weak, nonatomic) IBOutlet UIButton *myMedicineRightButton;
@property (strong, nonatomic) NSMutableArray *colorArray;
- (void)doneRemove:(id)sender;
- (void)showViewWithAnimation:(UIView *)view YPostion:(float)yPosition;
- (void)hideViewWithAnimation:(UIView *)view;

@end

@implementation timeController
{
    UIDatePicker *datePicker;
    UIToolbar *toolBar;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.fatherView];
    [self.view sendSubviewToBack:self.fatherView];
    [self.view bringSubviewToFront:_timeButton];
    [self.view bringSubviewToFront:_afterButton];
    [self.view bringSubviewToFront:_beforeButton];
//    [self.myMedicineView bringSubviewToFront:_myMedicineLeftButton];
//    [self.myMedicineView bringSubviewToFront:_myMedicineRightButton];
    
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy.MM.dd"];
    [_timeButton setTitle:[format stringFromDate:[NSDate date]] forState:UIControlStateNormal];

//    [self.navigationItem.backBarButtonItem setBackgroundImage:[UIImage imageNamed:@"navigationBarItem.jpg"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    UINavigationBar *bar = [[UINavigationBar alloc]init];
//    bar.translucent = NO;
    [bar setTintColor:[UIColor redColor]];
    //设置navigationbar 背景色
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0/255.0 green:172/255.0 blue:174/255.0 alpha:1.0]];
    
    // 设置为半透明
    
//    self.navigationController.navigationBar.translucent = NO;
    
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
//    [self.view addSubview:view];
//   // 设置的navigationBar的背景色覆盖了导航栏的button还有顶部缺少一排
//    [view.layer addSublayer:[self shadowAsInverse]]
    ;
   //设置navagation 标题字体的颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
    
    
    //设置timebutton的圆角框的颜色
    //给button加一个白色的板框

//    _timeButton.layer.borderColor = [[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0] CGColor];
//    _timeButton.layer.borderWidth = 1.0;
//   //给button设置弧度，这次设成圆角
//    _timeButton.layer.cornerRadius = 25.f;
//    _timeButton.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1.0];
//    _timeButton.layer.masksToBounds = YES;
//    [self.view addSubview:_timeButton];
}

//-(NSMutableArray *)colorArray{
//
//    if (!_colorArray) {
//        _colorArray = [NSMutableArray array];
//        CGFloat r = 0;
//        CGFloat g = 172;
//        CGFloat b = 174;
//        for (NSInteger i = 0; i < 25; i++) {
//            r = r+1.88;
//            g = g+1.12;
//            b = b+0.2;
//            CGColorRef color = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1].CGColor;
//            [_colorArray addObject:(__bridge id _Nonnull)(color)];
//        }
//    }
//    return _colorArray;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//-(CAGradientLayer *)shadowAsInverse{
//
//    CAGradientLayer *newShadow = [[CAGradientLayer alloc]init];
//    CGRect newShadowFeame = CGRectMake(0, 0, self.navigationController.navigationBar.bounds.size.width, self.navigationController.navigationBar.bounds.size.height);
//    newShadow.frame = newShadowFeame;
//    //添加逐渐改变颜色的数组（颜色透明度的改变）
//    newShadow.colors = self.colorArray;
//    return newShadow;
//
//
//}


- (IBAction)ShowRollerDate:(id)sender {
    
   


    
    if (_fatherView &&!toolBar && !datePicker) {// 这里需要判断, 因为toolbar, datePicker你只需要建立一次, 不需要重新创建
    
        toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.f, [UIScreen mainScreen].bounds.size.height -210.f-40.f, [UIScreen mainScreen].bounds.size.width, 40.f)];
        toolBar.barStyle = UIBarStyleBlackTranslucent;
        toolBar.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        [toolBar sizeToFit];
        
        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(doneRemove:)];
//        UIBarButtonItem *doneBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneRemove:)];
        UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil ];
        
        NSArray *array = [NSArray arrayWithObjects:flexibleSpaceLeft, doneBtn, nil ];
        [toolBar setItems:array animated:YES];
        
        [self.fatherView addSubview:toolBar];
        //[_fatherView bringSubviewToFront:toolBar];

        [self showViewWithAnimation:toolBar YPostion:([UIScreen mainScreen].bounds.size.height - 210.f - 40.f)];// 出现的时候需加入动画, 根据Y轴
    
        datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-210.f, [UIScreen mainScreen].bounds.size.width, 210.f)];
    //设置日期显示格式为中文
    datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
    
    //设置datepick的默认时间为当天
        datePicker.calendar = [NSCalendar currentCalendar];
       [datePicker setBackgroundColor:[UIColor whiteColor]];
        datePicker.datePickerMode = UIDatePickerModeDate;
        [_fatherView addSubview:datePicker];
       // [_fatherView bringSubviewToFront:datePicker];
        [self showViewWithAnimation:datePicker YPostion:([UIScreen mainScreen].bounds.size.height - 210.f)];// 出现的时候需加入动画, 根据Y轴
       [self.view bringSubviewToFront:_fatherView];
    NSLog(@"%@",@"执行成功");
    }else if ([toolBar isHidden] && [datePicker isHidden]){
        //显示
        
        [toolBar setHidden:NO];
        [datePicker setHidden:NO];
        [self showViewWithAnimation:toolBar YPostion:([UIScreen mainScreen].bounds.size.height - 210.f - 40.f)];// 出现的时候需加入动画, 根据Y轴
        [self showViewWithAnimation:datePicker YPostion:([UIScreen mainScreen].bounds.size.height - 210.f)];// 出现的时候需加入动画, 根据Y轴
        [self.view bringSubviewToFront:_fatherView];
        
    }
    
}
#pragma mark – performSegue
- (IBAction)toMyMedicineListControllerAction:(id)sender {
    
    [self performSegueWithIdentifier:@"toMyMedicineBoxController" sender:nil];
    
}




- (IBAction)beforeDayAction:(id)sender {
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy.MM.dd"];
    datePicker.date = [self preDate:datePicker.date];
    [_timeButton setTitle:[format stringFromDate:datePicker.date] forState:UIControlStateNormal];
    NSLog(@"%@",datePicker.date);
    
}

- (IBAction)afterDayAction:(id)sender {
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy.MM.dd"];
    datePicker.date = [self nextDate:datePicker.date];
    [_timeButton setTitle:[format stringFromDate:datePicker.date] forState:UIControlStateNormal];
}
//移除toolbar和datepicker
- (void)doneRemove:(id)sender {
    if (_fatherView && toolBar && datePicker) {
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"yyyy.MM.dd"];
        [_timeButton setTitle:[format stringFromDate:datePicker.date] forState:UIControlStateNormal];
        
        [self hideViewWithAnimation:toolBar];
        
        [self hideViewWithAnimation:datePicker];
        [self.view sendSubviewToBack:_fatherView];
    }
    
}

//显示动画
- (void)showViewWithAnimation:(UIView *)view YPostion:(float)yPosition
{
    CGRect rect_origin = view.frame;
    rect_origin.origin.y = [UIScreen mainScreen].bounds.size.height;
    view.frame = rect_origin;
    //    view.alpha = 0.5f;
    
    [UIView animateWithDuration:0.5f animations:^{
        CGRect rect_current = view.frame;
        rect_current.origin.y = yPosition;
        view.frame = rect_current;
        //        view.alpha = 1.0f;
    } completion:^(BOOL finished){
        
    }];
}

//隐藏动画
- (void)hideViewWithAnimation:(UIView *)view
{
    [UIView animateWithDuration:0.5f animations:^{
        CGRect rect_current = view.frame;
        rect_current.origin.y = ([UIScreen mainScreen].bounds.size.height);
        view.frame = rect_current;
    } completion:^(BOOL finished){
        [view setHidden:YES];
    }];
}

-(NSDate *)preDate:(NSDate *)date{

    NSDate *pre = [date dateByAddingTimeInterval:-(60*24*60)];
    return pre;

}
-(NSDate *)nextDate:(NSDate *)date{
    NSDate *next = [date dateByAddingTimeInterval:(60*24*60)];
    return next;

}

-(UIView *)fatherView{
    if(!_fatherView){
        _fatherView = [[UIView alloc]initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
       // [_fatherView setBackgroundColor:[UIColor redColor]];
    }

    return _fatherView;

}

    @end
 

